package com.example.login.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/db_user";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "123456";

    public boolean validateUser(User user) throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement("SELECT password FROM tb_users WHERE username =?")) {
            pstmt.setString(1, user.getUsername());
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    String storedPassword = rs.getString("password");
                    // 简单的密码验证，实际应用中应使用更安全的密码加密与验证方式
                    if (storedPassword.equals(user.getPassword())) {
                        return true;
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
